package com.rd.student.ruanjian.class4.zhouzihe.day0803;

import java.util.ArrayList;
import java.util.Collections;

public class Number implements Comparable {
    public static void main(String[] args) {
        ArrayList<Number> arrayList = new ArrayList();
        Number num1 = new Number(15);
        Number num2 = new Number(50);
        Number num3 = new Number(25);
        Number num4 = new Number(18);
        arrayList.add(num1);
        arrayList.add(num2);
        arrayList.add(num3);
        arrayList.add(num4);
        System.out.println("排序前:------");
        System.out.println(arrayList);
        System.out.println("排序后:------");
        Collections.sort(arrayList);
        System.out.println(arrayList);
        for (int i = 0; i < arrayList.size(); i++) {
            boolean isSorted = true;
            for (int j = 0; j < arrayList.size() - i; j++) {
                if ((arrayList.get(i).compareTo(arrayList.get(j)) > 0)) {
                    Number temp = arrayList.get(i);
                    arrayList.set(i, arrayList.get(j));
                    arrayList.set(j, temp);
                    isSorted = false;
                }
            }
            if (isSorted) break;

        }

    }

    int age;
    public Number(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Object o) {
        Number number = (Number) o;
        if (this.age > number.age) {
            return 1;
        } else if (this.age < number.age) {
            return -1;
        }
        return 0;
    }
    @Override
    public String toString() {
        return "age=" + age ;
    }
}
